package tacos.web.api;
import java.util.Date;
import java.util.List;
import java.util.Collection;
import java.util.ArrayList;

import org.springframework.hateoas.RepresentationModel;
import org.springframework.hateoas.CollectionModel;
import org.springframework.hateoas.server.core.Relation;

import lombok.Getter;
import tacos.Taco;

@Relation(value="taco", collectionRelation="tacos")
public class TacoResource extends RepresentationModel<TacoResource> {

  private static final IngredientResourceAssembler 
            ingredientAssembler = new IngredientResourceAssembler();
  
  @Getter
  private final String name;

  @Getter
  private final Date createdAt;

  @Getter
  private final Collection<IngredientResource> ingredients;
  
  public TacoResource(Taco taco) {
    this.name = taco.getName();
    this.createdAt = taco.getCreatedAt();

/*  // 直接使用CollectionModel<IngredientResource>的话，添加进去的就是_embedded节点，而不是ingredients列表
    this.ingredients = ingredientAssembler.toCollectionModel(taco.getIngredients());
*/    
    this.ingredients = ingredientAssembler.toCollectionModel(taco.getIngredients()).getContent();
      
/*   // 不需要转成List 
    this.ingredients = new ArrayList<IngredientResource>(
      ingredientAssembler.toCollectionModel(taco.getIngredients()).
      getContent());
*/
  }
  
}
